<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" :request-api="getArticleCateList">
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button v-auth="'article_cate_save'" type="primary" :icon="CirclePlus" @click="openDrawer('新增')">
          新增文章分类
        </el-button>
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button v-auth="'article_cate_save'" type="primary" link :icon="EditPen" @click="openDrawer('编辑', scope.row)">
          编辑
        </el-button>
        <el-button v-auth="'article_cate_del'" type="danger" link :icon="Delete" @click="deleteArticleCate(scope.row)">
          删除
        </el-button>
      </template>
    </ProTable>
    <ArticleCateEdit ref="drawerRef" />
  </div>
</template>
<script setup lang="tsx" name="roleManage">
import { ref, reactive } from "vue";
import { Cms } from "@/api/interface";
import ProTable from "@/components/ProTable/index.vue";
import ArticleCateEdit from "@/views/cms/articlecate/ArticleCateEdit.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { CirclePlus, Delete, EditPen } from "@element-plus/icons-vue";
import { getArticleCateList, saveArticleCate, delArticleCate, changeArticleCateStatus } from "@/api/modules/cms";
import { userStatus } from "@/utils/dict";
import { useAuthButtons } from "@/hooks/useAuthButtons";
import { useHandleData } from "@/hooks/useHandleData";
// ProTable 实例
const proTable = ref<ProTableInstance>();
//按钮权限
const { BUTTONS } = useAuthButtons();
// 表格配置项
const columns = reactive<ColumnProps<Cms.ArticleCate>[]>([
  { prop: "name", label: "分类名称", search: { el: "input" } },
  { prop: "code", label: "分类标识" },
  {
    prop: "status",
    label: "状态",
    tag: true,
    enum: userStatus,
    fieldNames: { label: "label", value: "value" },
    render: scope => {
      return (
        <>
          {BUTTONS.value.article_cate_status ? (
            <el-switch
              model-value={scope.row.status}
              active-text={scope.row.status ? "启用" : "禁用"}
              active-value={1}
              inactive-value={0}
              onClick={() => changeStatus(scope.row)}
            />
          ) : (
            <el-tag type={scope.row.status ? "success" : "danger"}>{scope.row.status ? "启用" : "禁用"}</el-tag>
          )}
        </>
      );
    }
  },
  { prop: "createdAt", label: "创建时间" },
  { prop: "updatedAt", label: "更新时间" },
  { prop: "operation", label: "操作", width: 250, fixed: "right" }
]);
//切换角色状态
const changeStatus = async (row: Cms.ArticleCate) => {
  await useHandleData(
    changeArticleCateStatus,
    { id: row.id, status: row.status == 1 ? 0 : 1 },
    `切换【${row.name}】文章分类状态`
  );
  proTable.value?.getTableList();
};

// 删除权限资源
const deleteArticleCate = async (params: Cms.ArticleCate) => {
  await useHandleData(delArticleCate, { codes: params.code }, `删除【${params.name}】文章分类`);
  proTable.value?.getTableList();
};

// 打开 drawer(新增、查看、编辑)
const drawerRef = ref<InstanceType<typeof ArticleCateEdit> | null>(null);
const openDrawer = (title: string, row: Partial<Cms.ArticleCate> = {}) => {
  const params = {
    title,
    isView: title === "查看",
    row: { ...row },
    api: saveArticleCate,
    getTableList: proTable.value?.getTableList
  };
  drawerRef.value?.acceptParams(params);
};
</script>
